<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Google Maps Geocoverage</title>
        <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
        <script type="text/javascript" src="wscaller.js"></script>
        <script type="text/javascript" src="http://maps.google.fr/maps?file=api&amp;v=2&amp;key=ABQIAAAAxp5n3zxRE-TTKUOFhef47xT-B4izgjS_4E6TSz2yzRQL-TGpGxT7QMA_IxFD9fbm3iZP6WUuDBSniw"></script>
        <script type="text/javascript">
var i=25;
$(document).ready(function()
{
    $.ajaxSetup({ global: false, type: "GET", url: "http://192.168.0.16:8080/geoserver/ows" });
    map = new GMap2(document.getElementById("mapdiv"));
    processNextCity();
});

function processNextCity()     
{  
    if(i==0)
    {
        GUnload();
        location.reload() ; 
    }
    i=i-1;
    GetNextCity("GM", function(guid, y, x)
    {
        processZoomLevel(guid, y, x, 20,'a');
    }, LogMsg);
}

function processZoomLevel(guid, y, x, zoom, typemap)
{
    $("#mapdiv").css("width","50px");
    $("#mapdiv").css("height","50px");
    map.setCenter(new GLatLng(y, x), zoom);
     if (typemap == 'a')
         map.setMapType(G_SATELLITE_MAP);
    else 
         map.setMapType(G_NORMAL_MAP);
    if(typemap == 'r' && zoom >= 17)
    {
        $("#mapdiv").css("width","800px");
        $("#mapdiv").css("height","800px");
    }
    window.setTimeout(processMap, 2000, guid, zoom, typemap, function()
    {
//        if (typemap =='a')
//        {
//            typemap = 'r';
//        }
//        else
//        {
            zoom = zoom-1;
//            typemap = 'a';
//        }
        if(zoom>8)
        {
            processZoomLevel(guid, y, x, zoom, typemap);
        }
        else
        {
            processNextCity();
        }
    }, 10);
}

function processMap(guid, zoom, typemap, callback, retry_count)
{
    tiles = '';
    if (typemap == 'a' || zoom >= 17)
    {
        tilestable=GetTiles(typemap);
        if(typemap == 'r' && zoom == 17)
            nb_expected_tiles=16;
        else
            nb_expected_tiles=1;
        if (tilestable.length >= nb_expected_tiles ) 
            tiles=tilestable.join("|");
    }
    LogMsg("tiles=##### "+tiles+" ####### typemap=" + typemap+" zoom="+ zoom);
    if (tiles == '' && retry_count>0 && (typemap == 'a' || zoom  >= 17))
    {
        window.setTimeout(processMap, 1000, guid, zoom, typemap, callback, retry_count-1); //passing parameters to setTimeOut works only in FireFox
    }
    else if(retry_count>0)
    {
        cp = GetCopyright();
        if (typemap == 'a')
        {
            res = GetResolution();
            SendToServer(guid, zoom, typemap, tiles, cp, '', res, callback);
        }
        else
        {
            GetStreetView(function(st)
            {
                SendToServer(guid, zoom, typemap, tiles, cp, st, '', callback);
            });
        }
    }
    else
    {
        LogMsg("Tiles did not load after retries !");
        callback();
    }
}

function GetTiles()
{
    tab = new Array();
    j=0;
    $("#mapdiv > div:first > div:first > div:eq(1) > div:first > img").each(function()
    {
        tab[j++]=$(this).attr("src");
    });
    
    return tab;
}

function GetCopyright()
{
    copyright = $("#mapdiv > .gmnoprint > span:last").text();
    return copyright;
}

function GetStreetView(callback)
{
    panoClient = new GStreetviewClient();
    panoClient.getNearestPanoramaLatLng(map.getCenter(), function(latlongpano)
    {
        if (latlongpano == null)
        {
            callback('');
        }
        else
        {
            lat = latlongpano.lat();
            longi = latlongpano.lng();
            diff_lat = Math.abs(lat-map.getCenter().lat());
            diff_long = Math.abs(longi-map.getCenter().lng());
            if (diff_lat<0.01 && diff_long<0.01)
            {
                callback('true');
            }
            else
                callback('');
        }
    });    
}

function GetResolution()
{
    view = map.getBounds();
    bottomright = view.getSouthWest();
    topleft = view.getNorthEast();
    
    res = ((topleft.lat() - bottomright.lat())*60*1852)/800;
    return res;
}
       
function SendToServer(guid, zoom, typemap,tiles, cp, st, res, callback)
{
    SendTilesToServer(guid, zoom,typemap, tiles, function()
    {
        SendResultToServer(guid, zoom,typemap, 3, cp, 0, function()
        {
            SendResultToServer(guid, zoom,typemap, 6, st, 0, function()
            {
                SendResultToServer(guid, zoom,typemap, 4, res, 2, function()
                {
                    callback();
                }, LogMsg );
            }, LogMsg );
        }, LogMsg );
    }, LogMsg );
}
        </script>
    </head>
    <body>
        <div id='mapdiv' style="position:relative; width:50px; height:50px;"></div>
    </body>
</html>
